package cn.xuqiudong.rpc.demo.provider.service;

import org.springframework.beans.factory.annotation.Value;
import cn.xuqiudong.rpc.demo.common.api.DemoInterface;
import cn.xuqiudong.rpc.demo.common.model.OneModel;
import cn.xuqiudong.rpc.spring.annotation.XqdService;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:服务提供者1
 * @author Vic.xu
 * @date 2022-03-01 17:51
 */
@XqdService
public class DemoProviderService implements DemoInterface {

    @Value("${server.name}")
    private String where;

    @Value("${server.port}")
    private int port;


    private String name(String name) {
        return name + " come from [" + where + ":" + port + "]";
    }

    @Override
    public List<OneModel> list(int num) {
        List<OneModel> list = new ArrayList<>();
        num = Math.min(num, 10);
        for (int i = 0; i < num; i++) {
            OneModel random = OneModel.random();
            random.setName(name(random.getName()));
            list.add(OneModel.random());
        }
        return list;
    }

    @Override
    public OneModel detail(int id) {
        OneModel random = OneModel.random();
        random.setName(name(random.getName()));
        random.setId(id);
        return random;
    }

    @Override
    public OneModel search(String name) {
        OneModel random = OneModel.random();
        random.setName(name(name));
        return random;
    }
}
